<?php
include '../models/AddWorksModel.php';
$title = $_POST["title"];
$medium = $_POST["medium"];
$dim = $_POST["dim"];
$description = $_POST["descript"];
$filelocation = "";
$filelocation2 = "";
$add = new AddWorksModel();

if (($_FILES["file"]["type"][0] == "image/gif") || ($_FILES["file"]["type"][0] == "image/jpeg") || ($_FILES["file"]["type"][0] == "image/jpg")
|| ($_FILES["file"]["type"][0] == "image/pjpeg") || ($_FILES["file"]["type"][0] == "image/png")){
	if ($_FILES["file"]["error"][0] > 0) {
   		echo "Return Code: " . $_FILES["file"]["error"][0] . "<br />"; 
	} else {
		if (file_exists("../images/artwork/" . $_FILES["file"]["name"][0])) {
	      echo $_FILES["file"]["name"][0] . " already exists. ";
		} else {
			move_uploaded_file($_FILES["file"]["tmp_name"][0],
	      	"../images/artwork/" . $_FILES["file"]["name"][0]);
	     	 $filelocation = "images/artwork/" . $_FILES["file"]["name"][0];
		}
 	}
} else {
  header("Location: ../add_works.php?message=error");
}

if (($_FILES["file"]["type"][1] == "image/gif") || ($_FILES["file"]["type"][1] == "image/jpeg") || ($_FILES["file"]["type"][1] == "image/jpg")
		|| ($_FILES["file"]["type"][1] == "image/pjpeg") || ($_FILES["file"]["type"][1] == "image/png")){
	if ($_FILES["file"]["error"][1] > 0) {
		echo "Return Code: " . $_FILES["file"]["error"][1] . "<br />";
	} else {
		if (file_exists("../images/artwork/thumbnails/" . $_FILES["file"]["name"][1])) {
			echo $_FILES["file"]["name"][1] . " already exists. ";
		} else {
			move_uploaded_file($_FILES["file"]["tmp_name"][1],
					"../images/artwork/thumbnails/" . $_FILES["file"]["name"][1]);
			$filelocation2 = "images/artwork/thumbnails/" . $_FILES["file"]["name"][1];
		}
	}
} else {
	header("Location: ../add_works.php?message=error");
}

if($add->addArtwork($title,$medium,$dim,$filelocation,$description,$filelocation2) == "true") {
	header("Location: ../collection.php?message=success");
} else {
	header("Location: ../add_works.php?message=error");
}
?>